package com.qf.timer;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.admin.domain.Jpush;
import com.qf.admin.domain.SendMessage;
import com.qf.admin.mapper.JpushMapper;
import com.qf.admin.mapper.MessageMapper;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author He
 * @Date: 2020/10/23 15:34
 * @Description :
 */
@Component
public class MessageDelay {
    public static final String APP_KEY = "9e8f37e14b228e89e618039f";
    public static final String MASTER_SECRET = "b3fc7efa1f3cc48b0fb574ce";

    private JpushMapper jpushMapper;
    private MessageMapper messageMapper;

    public MessageDelay(JpushMapper jpushMapper, MessageMapper messageMapper) {
        this.jpushMapper = jpushMapper;
        this.messageMapper = messageMapper;
    }

    private Logger logger = Logger.getLogger(MessageDelay.class);

    public void judgeTime() {
        logger.info("判断消息推送时间");
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, -1);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.gt("push_time", new Date());
        wrapper.lt("push_time",nowTime);
        wrapper.eq("status", 2);
        SendMessage sendMessage = messageMapper.selectOne(wrapper);
        if (null != sendMessage) {
            sendMessage.setStatus(1);
            messageMapper.updateById(sendMessage);
            Map<String, String> parms = new HashMap<>(8);
            parms.put("id", null);
            parms.put("msg", sendMessage.getContent());
            if (1 < sendMessage.getUserId()) {
                Jpush jpush = jpushMapper.selectById(sendMessage.getUserId());
                if (null != jpush) {
                    String regId = jpush.getRegId();
                    parms.put("id", regId);
                }

            }
            System.out.println("成功");
            jpushAndroid(parms, sendMessage.getTitle());
        }

    }

    //极光推送
    public static void  jpushAndroid(Map<String, String> parm, String title) {
        //创建JPushClient(极光推送的实例)
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //推送的关键,构造一个payload
        PushPayload payload = null;
        if (null != parm.get("id") && !"".equals(parm.get("id").trim())) {
            payload = PushPayload.newBuilder()
                    .setPlatform(Platform.android())
                    .setAudience(Audience.all())
                    .setAudience(Audience.registrationId(parm.get("id")))
                    .setNotification(Notification.android(parm.get("msg"), title, parm))
                    //发送内容
                    .setOptions(Options.newBuilder().setApnsProduction(false).build())
                    //这里是指定开发环境,不用设置也没关系
                    .setMessage(Message.content(parm.get("msg")))
                    .build();
        } else {
            payload = PushPayload.newBuilder()
                    //指定android平台的用户
                    .setPlatform(Platform.android())
                    //你项目中的所有用户
                    .setAudience(Audience.all())
                    .setNotification(Notification.android(parm.get("msg"), title, parm))
                    //发送内容
                    .setOptions(Options.newBuilder().setApnsProduction(false).build())
                    //这里是指定开发环境,不用设置也没关系
                    .setMessage(Message.content(parm.get("msg")))
                    .build();
        }
        try {
            PushResult pu = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }
}
